<h3><?php echo $title ?></h3>

<strong><?php
	//echo $today;
	
	//echo $end_date;
	if(strtotime($today) < strtotime($end_date)){
		if ($isSubuser){
			if ($privileges->row()->invite_respondents){
				echo anchor('user/invite_resp'.'/'.$id.'/'.$survey_id,'Invite more respondents'); echo "   |   ";
			}
			echo anchor('user/end_survey'.'/'.$id.'/'.$survey_id,'End Survey'); echo "   |   ";
			if ($privileges->row()->view_reports){
				echo anchor('user/view_survey_report'.'/'.$id.'/'.$survey_id,'View Report'); echo "<br />";
			}
			
		}
		else{
			echo anchor('user/invite_resp'.'/'.$id.'/'.$survey_id,'Invite more respondents'); echo "   |   ";	
			echo anchor('user/end_survey'.'/'.$id.'/'.$survey_id,'End Survey'); echo "   |   ";	
			echo anchor('user/view_survey_report'.'/'.$id.'/'.$survey_id,'View Report'); echo "<br />";		
		}

				
	}
	else{
		echo anchor('user/view_survey_report'.'/'.$id.'/'.$survey_id,'View Report'); echo "<br />";
		echo "This survey has already ended. To reactivate this survey,extend the end date.";
	}

	
?></strong><br>	
<?php


	if (strtotime($today) < strtotime($end_date)){
		if ($isSubuser){
			if ($privileges->row()->edit_questions){
				echo anchor('user/add_question/'.$survey_id.'/'.$id,'Add a question'); echo "   |   ";
				echo anchor('user/delete_questions/'.$survey_id.'/'.$id,'Delete a Question'); echo "   |   ";
				echo anchor('user/update_questions/'.$survey_id.'/'.$id,'Update a Question');			
			}
		}
		else{
			echo anchor('user/add_question/'.$survey_id.'/'.$id,'Add a question'); echo "   |   ";
			echo anchor('user/delete_questions/'.$survey_id.'/'.$id,'Delete a Question'); echo "   |   ";
			echo anchor('user/update_questions/'.$survey_id.'/'.$id,'Update a Question');		
		}
	
	}
	else{
	}

	echo "<br>";	
?>

<?php
$count = 1;
$initially_empty = FALSE;

if(sizeof($categories) == 0)
{
	$categories = array();
	$initially_empty = TRUE;
	$pages = ceil(sizeof($questions)/5);	
	for($i = 0; $i < sizeof($questions); $i++)
	{
		if($i % 5 == 0)
		{
			$page = ceil(($i+1)/5);
			$categories[] = array("category_name" => "Part $page of $pages");
		}		
		$questions[$i]['category_name'] = "Part $page of $pages";
	}
}

foreach($categories as $category)
{
	$question_number = 1;
	if($count==1)
	{
		echo "<div id=\"category$count\">";
		
	}
	else
	{
		echo "<div id=\"category$count\" style=\"display:none\">";
	}
	echo "<h3>".$category['category_name']."</h3>";
	
	$count++;
		
	foreach($questions as $item)
	{
		if($item['category_name'] == $category['category_name'])
		{
			$temp = "question".$item['question_number'];
			echo $question_number++.". ".$item['body']."<br/>";
			if($item['type'] == "text")
			{		
				$data = array(
				      'name'        => "question".$item['question_number'],
				      'id'          => "question".$item['question_number'],
				      'value'       => "",
				      'maxlength'   => '100',
				      'size'        => '50',
				      'style'       => 'width:50%;margin:10px',
				    );

				echo form_input($data);
				echo "<br/>";
			}
	
	
			if($item['type'] == "paragraph text")
			{
				$data = array(
				      'name'        => "question".$item['question_number'],
				      'id'          => "question".$item['question_number'],
				      'value'      => "",
				      'rows'   => '3',
				      'style'       => 'width:50%;margin:10px',
				    );
				echo form_textarea($data);
				echo "<br>";
			}
	
			if($item['type'] == "multiple choice")
			{
				foreach($item['choices'] as $choice)
				{
					$checked = FALSE;
					$data = array(
					'name'        => "question".$item['question_number'],
					'id'          => "question".$item['question_number'],
					'value'       => $choice,
					'checked'     => $checked,
					'style'       => 'margin:10px',
					);

					echo form_radio($data). $choice."<br/>";
				}
			}
	
			if($item['type'] == "checkboxes")
			{
				?>
				<script type="text/javascript">
					function chkcontrol(j,limit)
					{
						var total=0;
						for(var i=0; i < document.survey.<?php echo "question".$item['question_number']; ?>.length; i++)			{
							if(document.survey.<?php echo "question".$item['question_number']; ?>[i].checked){
								total =total +1;}
								if(total > limit){
								document.survey.<?php echo "question".$item['question_number']; ?>[j].checked = false ;
					
			return false;
							}
						}
					}
				</script>
				<?php
				$temp = "question".$item['question_number'];
				$checked = FALSE;
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					$selected = FALSE;
					if($checked!=FALSE)
					{
						$selected = in_array($item['choices'][$i], $checked);
					}
					$js = "onClick=\"chkcontrol(".($i-1).",".$item['choice_limit'].","."question".$item['question_number'].")\";";
					$data = array(
					'name'        => "question".$item['question_number']."[]",
		
			'id'          => "question".$item['question_number'],
					'value'       => $item['choices'][$i],
					'checked'     => $selected,
					'style'       => 'margin:10px',
					);

					echo form_checkbox($data,NULL, FALSE, $js ). $item['choices'][$i]."<br/>";
				}
			}
	
			if($item['type'] == "choose")
			{
				$js = "id="."question".$item['question_number'];
				$choices = array(""=>"");
				foreach($item['choices'] as $choice)
				{
					$choices[$choice] = $choice;
				}
				echo "&nbsp;&nbsp;&nbsp;".form_dropdown("question".$item['question_number'], $choices, NULL, $js);
			}
	
			if($item['type'] == "scale")
			{
				echo "<br>";
	
			for($i = 1; $i<=strlen($item['choices'][1]) + 10; $i++)
				{
					echo "&nbsp;";
				}
		
				for($choice = 1; $choice<=5; $choice++)
				{
					echo $choice."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				echo "<br/>";
			
				echo "&nbsp;&nbsp;&nbsp;".$item['choices'][1]."&nbsp;&nbsp;&nbsp;&nbsp;";
	
			for($choice = 1; $choice<=5; $choice++)
				{
					$data = array(
					'name'        => "question".$item['question_number'],
					'id'          => "question".$item['question_number'],
					'value'       => $choice,
					'checked'     => FALSE,
					);

					echo form_radio($data)."&nbsp;&nbsp;&nbsp;&nbsp;";
				}
				echo $item['choices'][2]."<br>";
			}
	
			if($item['type'] == "ranking")
			{
				echo "<table colspan=\"2\"><tr><td><table>";
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					echo "<tr><td>".$i."</td></tr>";
				}
				echo "</table></td><td>";
		
				echo "<table id=\"question".$item['question_number']."\">";
		
				for($i=1; $i<=sizeof($item['choices']);$i++)
				{
					$temp = "question".$item['question_number'].$i;
					echo "<tr id=\"question".$item['question_number']."option".$i."\"><td>".$item['choices'][$i]."</td></tr>";
							
					echo "<input type=\"hidden\" name=\"question".$item['question_number'].$i."\" id=\"question".$item['question_number'].$i."\" value=\"".$item['choices'][$i]."\">";
				}

				echo "</table></td></tr></table>";
				?>
		
				<script type="text/javascript" src="http://localhost/pollwatcher/public/js/jquery.tablednd_0_5.js"></script>
				<script type="text/javascript">
					$(document).ready(function() {
						$("#<?php echo "question".$item['question_number'];?>").tableDnD(
							{
								onDragClass: "myDragClass",
								onDrop: function(table, row) {
									var rows = table.tBodies[0].rows;
						
									for (var i=0; i<rows.length; i++) {
										$("input[name=<?php echo "question".$item['question_number']; ?>"+(i+1)+"]").val((i+1) + " " + $("#"+rows[i].id + " > td" ).text());
									}
								}
							}
						);
					});
					</script>
				<?php
		
			}
	
			if($item['with_text'] ==1)
			{
				$text_name = "text".$item['question_number'];
				echo "&nbsp;&nbsp;".$item['text_label'];
				$data = array(
				      'name'        => "text".$item['question_number'],
				      'id'          => "text".$item['question_number'],
				      'value'       => '',
				      'style'       => 'width:30%;margin:10px',
				    );
				echo form_input($data);
			}
		
			echo "<br/><br/>";
		}
	}
	echo "</div>";
}


?>

<?php 
if($initially_empty == FALSE || sizeof($questions) > 5)
{
?>

	<a id="back">Back</a>
	<a id="next">Next</a> <br/>

	<script type="text/javascript">
	$(document).ready(function(){
		$("#back").hide();
		var current = 1;
		var limit = <?php echo sizeof($categories); ?>;
		$("#next").click( function(){
			if(current == 1)
			{
				$("#back").show();
			}
		
			if(current == limit - 1)
			{
				$("#next").hide();
			}
		
			if(current < limit)
			{
				$("#category"+ current).hide("slow");
				current = current + 1;
				$("#category"+ current).show("slow");
			}	
		});
	
		$("#back").click( function(){
			if(current == 2)
			{
				$("#back").hide();
			}
		
			if(current == limit)
			{
				$("#next").show();
			}
				
			if(current > 1)
			{
				$("#category"+ current).hide("slow");
				current = current - 1;
				$("#category"+ current).show("slow");
			}
					
		});
	});
	</script>
<?php
}
?>





